
package com.nn.smart.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nn.smart.bus.commons.mybatis.service.impl.BaseServiceImpl;
import com.nn.smart.bus.commons.tools.constant.NumConstant;
import com.nn.smart.bus.commons.tools.utils.ConvertUtils;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.dao.AreaDao;
import com.nn.smart.bus.dao.SysDeptDao;
import com.nn.smart.bus.dto.SimpleAreaDTO;
import com.nn.smart.bus.dto.SysSimpleDeptDTO;
import com.nn.smart.bus.entity.AreaEntity;
import com.nn.smart.bus.redis.AreaRedis;
import com.nn.smart.bus.service.AreaService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @author nanning
 */
@Slf4j
@Service
public class AreaServiceImpl extends BaseServiceImpl<AreaDao, AreaEntity> implements AreaService {


    @Autowired
    private AreaRedis areaRedis;

    @Autowired
    private SysDeptDao deptDao;


    @Override
    public Result<List<SimpleAreaDTO>> listSimpleAreaInfo(String areaId) {

        List<SimpleAreaDTO> simpleAreaList = Lists.newArrayList();

        try {
            if (StringUtils.isNotBlank(areaId)) {

                List<SimpleAreaDTO> areaList = areaRedis.getSimpleAreaList(areaId);
                if (null != areaList) {
                    return new Result<List<SimpleAreaDTO>>().ok(areaList);
                }

                Integer pid = Integer.parseInt(areaId);
                QueryWrapper<AreaEntity> arWrapper = new QueryWrapper<>();
                arWrapper.select("ID", "NAME");
                arWrapper.eq("PARENT_ID", pid);
                arWrapper.eq("STATUS", NumConstant.ONE);
                arWrapper.orderByAsc("LEVEL", "SORT");
                List<AreaEntity> entityList = baseDao.selectList(arWrapper);
                if (!entityList.isEmpty()) {
                    simpleAreaList = ConvertUtils.sourceToTarget(entityList, SimpleAreaDTO.class);
                    areaRedis.setSimpleAreaList(areaId, simpleAreaList);
                }
            }
        } catch (Exception e) {
            log.error("获取区划信息失败，错误信息{}", e.getMessage());
        }
        return new Result<List<SimpleAreaDTO>>().ok(simpleAreaList);
    }

    @Override
    public Result<List<SysSimpleDeptDTO>> listSimpleDeptInfo(Map<String, Object> params) {

        List<SysSimpleDeptDTO> sysSimpleDeptS = this.deptDao.listSimpleDeptInfo(params);

        return new Result().ok(sysSimpleDeptS);
    }
}
